<template>
  <div>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="部门名称">
        <el-input
          v-model="searchModel.searchItem.depname"
          placeholder="部门名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>

        <el-button type="primary" @click="DepAdd">添加</el-button>

        <el-button type="primary" @click="DepDelete">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData.data" style="width: 100%" ref="cbk">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="dep_id" label="编号"> </el-table-column>
      <el-table-column prop="dep_name" label="部门名称"> </el-table-column>
      <el-table-column prop="dep_parent_name" label="上级部门">
      </el-table-column>
      <el-table-column prop="dep_isshow" label="部门列表">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="DepUpdateById(scope.row.dep_id)"
            size="small"
            >{{ scope.row.dep_isshow == 0 ? "显示" : "隐藏" }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="dep_masssage" label="部门描述"> </el-table-column>
      <el-table-column prop="dep_type_name" label="部门类型"> </el-table-column>
      <el-table-column prop="dep_order" label="排序"> </el-table-column>
      <el-table-column label="选择">
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.row.dep_id)"
            type="text"
            size="small"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="onSubmit"
      @current-change="onSubmit"
      :current-page.sync="searchModel.pageIndex"
      :page-sizes="[1, 3, 5, 10]"
      :page-size.sync="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total"
    >
    </el-pagination>

    <el-dialog
      title="修改"
      :visible.sync="isShowEdit"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="depInfo" label-width="80px">
        <el-form-item label="部门ID">
          <el-input v-model="depInfo.dep_id"></el-input>
        </el-form-item>
        <el-form-item label="上级部门">
          <el-select v-model="depInfo.dep_parent_id" placeholder="请选择">
            <el-option :key="0" label="请选择" :value="0">请选择</el-option>
            <el-option
              v-for="item in tableData.data"
              :key="item.dep_id"
              :label="item.dep_name"
              :value="item.dep_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门名称">
          <el-input v-model="depInfo.dep_name"></el-input>
        </el-form-item>
        <el-form-item label="部门类型">
          <el-select v-model="depInfo.dep_type_id" placeholder="请选择">
            <el-option :key="0" label="请选择" :value="0">请选择</el-option>
            <el-option
              v-for="item in DepTypeInfo"
              :key="item.dep_type_id"
              :label="item.dep_type_name"
              :value="item.dep_type_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门描述">
          <el-input v-model="depInfo.dep_masssage"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-radio-group v-model="depInfo.dep_isshow">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门排序">
          <el-input v-model="depInfo.dep_order"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmitUpdate">修 改</el-button>
          <el-button @click="isShowEdit = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog
      title="添加"
      :visible.sync="isShowAdd"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="depInfoAdd" label-width="80px">
        <el-form-item label="上级部门">
          <el-select v-model="depInfoAdd.dep_parent_id" placeholder="请选择">
            <el-option :key="0" label="请选择" :value="0">请选择</el-option>
            <el-option
              v-for="item in tableData.data"
              :key="item.dep_id"
              :label="item.dep_name"
              :value="item.dep_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门名称">
          <el-input v-model="depInfoAdd.dep_name"></el-input>
        </el-form-item>
        <el-form-item label="部门类型">
          <el-select v-model="depInfoAdd.dep_type_id" placeholder="请选择">
            <el-option :key="0" label="请选择" :value="0">请选择</el-option>
            <el-option
              v-for="item in DepTypeInfo"
              :key="item.dep_type_id"
              :label="item.dep_type_name"
              :value="item.dep_type_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门描述">
          <el-input v-model="depInfoAdd.dep_masssage"></el-input>
        </el-form-item>
        <el-form-item label="部门排序">
          <el-input v-model="depInfoAdd.dep_order"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmitAdd">添 加</el-button>
          <el-button @click="isShowAdd = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

 <script>
import {
  BindDepType,
  SearchPage,
  DepartmentAdd,
  GetDepartmentById,
  DepartmentUpdate,
  DepartmentDelete,
  DepartmentUpdateById,
} from "./DepartmentManagerApi";
export default {
  data() {
    return {
      DepTypeInfo: [],
      isShowEdit: false,
      isShowAdd: false,
      depInfoAdd: {},
      depInfo: {
        dep_type_id: 0,
      },
      searchModel: {
        searchItem: {
          depname: null,
        },
        pageIndex: 1,
        pageSize: 3,
      },
      tableData: [],
    };
  },
  created() {
    this.onSubmit();
  },
  methods: {
    onSubmitAdd() {
      DepartmentAdd(this.depInfoAdd).then((r) => {
        if (r.data) {
          this.$message("添加成功");
          this.isShowAdd = false;
          this.onSubmit();
        }
      });
    },
    onSubmitUpdate() {
      DepartmentUpdate(this.depInfo).then((r) => {
        if (r.data) {
          this.$message("修改成功");
          this.isShowEdit = false;
          this.onSubmit();
        }
      });
    },
    DepUpdateById(row) {
      DepartmentUpdateById(row).then((r) => {
        if (r.data) {
          this.$message("状态修改成功");
          this.onSubmit();
        }
      });
    },
    DepDelete() {
      var arr = this.$refs.cbk.selection;
      if (arr.length == 0) {
        this.$message("请选择至少一条数据");
        return;
      }
      var ids = new Array();
      arr.forEach((obj) => {
        ids.push(obj.dep_id);
      });
      DepartmentDelete(ids).then((r) => {
        if (r.data) {
          this.$message("删除成功");
          this.onSubmit();
        }
      });
    },

    onSubmit() {
      SearchPage(this.searchModel).then((r) => {
        this.tableData = r.data;
      });
    },
    DepAdd() {
      this.isShowAdd = true;
      BindDepType().then((r) => {
        this.DepTypeInfo = r.data;
      });
    },
    handleClick(row) {
      this.isShowEdit = true;
      GetDepartmentById(row).then((r) => {
        this.depInfo = r.data;
      });
      BindDepType().then((r) => {
        this.DepTypeInfo = r.data;
      });
    },
  },
};
</script>
